package com.iflyun.goodsshow.controller;

import java.util.HashMap;

import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iflyun.goodsshow.entity.User;
import com.iflyun.goodsshow.service.UserService;

import net.sf.json.JSONObject;

@RequestMapping(value = "/user")
@Controller
public class UserController {

	@Autowired
	public UserService userService;

	@RequestMapping("/getUser")
	@ResponseBody
	public String getUser(String name, String password, HttpSession session) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		HashMap<String, Object> map = new HashMap<String, Object>();
		User user = userService.getUser(name, password);
		map.put("user", user);
		if (StringUtils.isNotBlank(user.getName())) {
			result.put("status", "1");
			result.put("message", "登录成功");
			session.setAttribute("user", user);
		} else {
			result.put("status", "0");
			result.put("message", "账号或密码错误");
		}
		JSONObject json = JSONObject.fromObject(result);
		return json.toString();

	}
	
	/**
	 * 用户退出
	 */
	@RequestMapping(value="/exit")
	public String userExit(HttpSession session){
		if(session.getAttribute("user")!=null){
			session.invalidate();
		}
		return "index";
	}

}
